﻿using UnityEngine;
using System.Collections;
using Assets.Scripts;

public class OpponentController : MonoBehaviour {

	public GameObject ball;
	public Boundary boundary;
	private float speed;

    /// <summary>
    /// Called at the creation of the game.
    /// </summary>
    void Start()
    {
        speed = 0.05f;

        // set the difficulty by changing the speed of the IA player
        switch (PlayerPrefs.GetString("difficulty"))
        {
            case "Easy":
                speed = 0.05f;
                break;
            case "Medium":
                speed = 0.075f;
                break;
            case "Hard":
                speed = 0.1f;
                break;
        }
    }
	
	
    /// <summary>
    /// Updates this instance.
    /// Update is called once per frame
    /// </summary>
	void Update () {

            // move the IA player depending on the position of the ball

			if (ball.transform.position.x > this.transform.position.x) {
				transform.position = new Vector3(transform.position.x  + speed, transform.position.y, transform.position.z);
			}

			if (ball.transform.position.y > this.transform.position.y) {
				transform.position = new Vector3(transform.position.x, transform.position.y + speed, transform.position.z);
			}

			if (ball.transform.position.x < this.transform.position.x) {
				transform.position = new Vector3(transform.position.x - speed, transform.position.y, transform.position.z);
			}

			if (ball.transform.position.y < this.transform.position.y) {
				transform.position = new Vector3(transform.position.x, transform.position.y - speed, transform.position.z);
			}

        // prevent the IA player from get out of the boundary
		rigidbody.position = new Vector3 (
			Mathf.Clamp(rigidbody.position.x, boundary.xMin, boundary.xMax),
			Mathf.Clamp(rigidbody.position.y, boundary.yMin, boundary.yMax),
			10f
		);
	}
}
